<template>
  <div class="modules-github">
    <a-modal
      :width="640"
      :visible="visible"
      :confirmLoading="loading"
      @ok="() => { $emit('ok') }"
      @cancel="() => { $emit('cancel') }"
      ok-text="保存"
      cancel-text="取消"
    >
      <a-spin :spinning="loading">
        <a-form :form="form" v-bind="formLayout">
          <a-form-item>
            <a-input v-decorator="['pid', { initialValue: pid }]" type="hidden" />
          </a-form-item>
          <a-form-item
            label="cdn加速"
          >
            <a-select v-decorator="['domain', {initialValue: 'cdn.jsdelivr.net'}]">
              <a-select-option value="cdn.jsdelivr.net">JsDeliver-推荐</a-select-option>
              <!-- <a-select-option value="fastgit">fastgit</a-select-option> -->
              <a-select-option value="raw.githubusercontent.com">GitHub-无加速</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item
            label="用户名"
          >
            <a-input placeholder="GitHub用户名" v-decorator="['username', {rules:[{required: true, message: '请填写GitHub用户名'}]}]" />
          </a-form-item>  
          <a-form-item
            label="仓库名"
          >
            <a-input placeholder="GitHub项目仓库名称" v-decorator="['repo', {rules:[{required: true, message: '请填写仓库名'}]}]" />
          </a-form-item>
          <a-form-item
            label="分支名"
          >
            <a-input v-decorator="['branch', { initialValue: 'master', rules:[{required: true, message: '请填写分支名'}]}]" />
          </a-form-item>
          <a-form-item
            label="token"
          >
            <a-input v-decorator="['token', {rules:[{required: true, message: '请填写token'}]}]" />
          </a-form-item>
          <a-form-item
            label="路径"
          >
            <a-input v-decorator="['path', {initialValue: 'images', rules:[{required: false, message: '请填写路径'}]}]" />
          </a-form-item>              
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import pick from 'lodash.pick'
import COMMON from '@/store/common'

// 表单字段
const fields = ['pid', 'domain', 'username', 'repo', 'branch', 'token', 'path']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      pid: COMMON.github,
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>
<style lang="less" scoped>
  /deep/ .ant-form-item {
    margin-bottom: 0px;
  }
</style> 